import type Database from 'better-sqlite3';

const migrateV3 = (db: Database.Database) => {
  // 创建临时表
  const createTempTableSQL = `
    CREATE TABLE "video_mark_info_temp" (
      "id" TEXT NOT NULL UNIQUE,
      "video_info_id" TEXT,
      "start_time" TEXT,
      "end_time" TEXT,
      "label" TEXT,
      "describe" TEXT,
      "channel" INTEGER,
      "status" INTEGER,
      "create_time" TEXT,
      "create_user" INTEGER,
      PRIMARY KEY ( "id" )
    );
  `;
  db.exec(createTempTableSQL);

  // 迁移数据
  const migrateDataSQL = `
    INSERT INTO "video_mark_info_temp" (id, video_info_id, start_time, end_time, label, describe, channel, status,create_time, create_user)
    SELECT id, video_info_id, start_time, end_time, label, describe, channel, status,create_time, create_user
    FROM "video_mark_info"
  `;
  db.exec(migrateDataSQL);

  // 删除旧表
  db.exec('DROP TABLE "video_mark_info"');

  // 重命名临时表为原表名
  db.exec('ALTER TABLE "video_mark_info_temp" RENAME TO "video_mark_info"');
};

export default migrateV3;
